<?php
/**
 * +-------------------------------------------------------------
 * |Caption:
 * +-------------------------------------------------------------
 * |Author: Lenny
 * +-------------------------------------------------------------
 * |Email:  1685964109@qq.com
 * +-------------------------------------------------------------
 * |Date:   2024/7/5 下午3:41
 * +-------------------------------------------------------------
 */

namespace app\admin\validate;

use think\Validate;

class StoreValidate extends Validate
{
    protected $rule = [
            'title'   => 'require|max:100',
            'address' => 'require|max:255',
            'bank'    => 'require|checkBank',
            'owners'  => 'require|checkOwners',
            'admin'   => 'require|checkAdmin',
    ];

    protected $message = [
            'title.require'      => '标题不能为空',
            'title.max'          => '标题最多不能超过100个字符',
            'address.require'    => '地址不能为空',
            'address.max'        => '地址最多不能超过255个字符',
            'bank.require'       => '账户信息不能为空',
            'owners.require'     => '负责人信息不能为空',
            'admin.require'      => '管理员信息不能为空',
            'bank.checkBank'     => '银行信息不完整',
            'owners.checkOwners' => '负责人信息不完整',
            'admin.checkAdmin'   => '管理员信息不完整',
    ];

    protected $scene = [
            'add'  => ['title', 'address', 'bank','owners','admin'],
    ];

    protected function checkBank($value): bool
    {
        $bankInfo =  json_decode($value,true);
        return !(empty($bankInfo['type']) || empty($bankInfo['branch']) || empty($bankInfo['no']));
    }

    protected function checkOwners($value): bool
    {
        $owners = json_decode($value,true);

        return !(empty($owners['username']) || empty($owners['phone']) || empty($owners['id_no']));
    }

    protected function checkAdmin($value): bool
    {
        $admin = json_decode($value,true);
        return !(empty($admin['username']) || empty($admin['password']));
    }
}